import json
import socket
import time
from multiprocessing import Queue
#
# # 设置服务器监听端口
# server_port = 12345
#
# # 创建一个套接字对象，绑定到指定端口并开始监听
# server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# server_socket.bind((socket.gethostbyname(socket.gethostname()), server_port))
# server_socket.listen(5)
#
# print("服务器已启动，等待客户端连接...")
#
# # 循环接收客户端连接请求
# while True:
#    # 接受客户端连接
#    client_socket, client_address = server_socket.accept()
#    print(f"客户端 {client_address} 已连接")
#
#    try:
#        # 与客户端通信
#        while True:
#            data = client_socket.recv(1024)
#            if not data:
#                print(f"客户端 {client_address} 已断开连接")
#                break
#            print(f"接收到数据：{data.decode()}")
#
#            # 向客户端发送数据
#            client_socket.sendall(data)
#
#    finally:
#        # 关闭客户端连接
#        client_socket.close()


class SocketServerMockClass:
    def __init__(self, shost: str, sport: int):
        self.host = shost
        self.port = sport
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def bind_server(self):
        self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_socket.bind((self.host, self.port))

    def listen(self):
        self.server_socket.listen(5)

    def start(self):
        self.bind_server()
        self.listen()
        print("服务器已启动，等待客户端连接...")
        return self.server_socket

    def run(self):
        socket_server = self.start()
        while True:
            print('--')
            client_socket, client_address = socket_server.accept()
            client_socket.setblocking(False)
            print('**')
            print(f"Connection from {client_address}")

            try:
                while True:
                    # data = client_socket.recv(1024)
                    # print(data)
                    # for i in range(20):
                        # byte_res = i.to_bytes((i.bit_length()+7)//8, byteorder='big')
                        # print(byte_res)
                        # byte_res = bytes(i)
                        # print(byte_res)
                        # print(i)
                        # 将整数类型的数据转换为字节串类型

                    res = b''
                    for i in range(20):
                        num_byte = i.to_bytes(1, byteorder='big')
                        res += num_byte
                    print('res', res)
                    client_socket.sendall(res)
                    # # i += 1
                    # time.sleep(0.1)
                    # num = 9
                    # num_byte = num.to_bytes(1, byteorder='big')
                    # client_socket.sendall(num_byte)
                    # time.sleep(1)

                    # break
            # print('over')
            finally:
                print('finally')
                client_socket.close()
        # print('second while')



if __name__ == '__main__':
    host = '192.168.211.182'
    port = 8080
    ss = SocketServerMockClass(host, port)
    ss.run()
    # socket_server = ss.start()
    # while True:
    #     client_socket, client_address = socket_server.accept()
    #     print(f"Connection from {client_address}")
    #
    #     try:
    #         while True:
    #             data = client_socket.recv(1024)
    #             if not data:
    #                 break
    #             print(f"Received: {data.decode()}")
    #
    #             data = client_socket.send(data)
    #             print(f"Sent: {data}")
    #
    #     finally:
    #         client_socket.close()















